from flask import jsonify, Blueprint
from flask import request
from flask_jwt import jwt_required

from db import *

indexx = Blueprint("index", __name__)

@indexx.route('/get_index')
@jwt_required()
def get_hot_ip():
    ret = []
    use_all_1 = stock_db.find({"use": 1}).count()
    ret.append({"key": 'use_all_1', "num": use_all_1, 'msg': '市值13-50'})
    use_all_2 = stock_db.find({"use": 2}).count()
    ret.append({"key": 'use_all_2', "num": use_all_2, 'msg': '市值50-100'})
    use_all_3 = stock_db.find({"use": 3}).count()
    ret.append({"key": 'use_all_3', "num": use_all_3, 'msg': '市值100-400'})
    use_all_4 = stock_db.find({"use": 4}).count()
    ret.append({"key": 'use_all_4', "num": use_all_4, 'msg': '市值400-800'})
    use_all_5 = stock_db.find({"use": 5}).count()
    ret.append({"key": 'use_all_5', "num": use_all_5, 'msg': '市值800以上'})


    use_today_0 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 0}).count()
    ret.append({"key": 'use_today_0', "num": use_today_0})
    use_today_1 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 1}).count()
    ret.append({"key": 'use_today_1', "num": use_today_1})
    use_today_2 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 2}).count()
    ret.append({"key": 'use_today_2', "num": use_today_2})
    use_today_3 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 3}).count()
    ret.append({"key": 'use_today_3', "num": use_today_3})
    use_today_4 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 4}).count()
    ret.append({"key": 'use_today_4', "num": use_today_4})

    use_today_10 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 10}).count()
    ret.append({"key": 'use_today_10', "num": use_today_10, 'msg': '作日涨幅小于-6'})
    use_today_11 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 11}).count()
    ret.append({"key": 'use_today_11', "num": use_today_11, 'msg': '核心题材不满足'})
    use_today_12 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 12}).count()
    ret.append({"key": 'use_today_12', "num": use_today_12, 'msg': '连续4天小于-1.5'})
    use_today_13 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 13}).count()
    ret.append({"key": 'use_today_13', "num": use_today_13, 'msg': '前两日加起来-10'})
    use_today_14 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 14}).count()
    ret.append({"key": 'use_today_14', "num": use_today_14, 'msg': '十日加起来15'})
    use_today_15 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 15}).count()
    ret.append({"key": 'use_today_15', "num": use_today_15, 'msg': '十日加起来-12'})
    use_today_16 = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 16}).count()
    ret.append({"key": 'use_today_16', "num": use_today_16, 'msg': '十日内出现了-6'})

    yes = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 2, 'yesterday': 1}).count()
    byes = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": 2, 'byesterday': 1}).count()
    ret.append({"key": 'yesterday', "num": yes})
    ret.append({"key": 'byesterday', "num": byes})



    hot_ip = new_ip_db.find().count()
    ret.append({"key": 'hot_ip', "num": hot_ip})
    new_ip = new_ip_txt_db.find().count()
    ret.append({"key": 'new_ip', "num": new_ip})
    dirty_ip = dirty_ip_db.find().count()
    ret.append({"key": 'dirty_ip', "num": dirty_ip})

    return jsonify(ret)

@indexx.route('/get_stock_detail')
@jwt_required()
def get_stock_detail():
    symbol = request.args.get('symbol')
    if not symbol:
        return jsonify([{"key": 'OK'}])
    stock = stock_db.find_one({"symbol": symbol})
    stock.pop("_id")
    five_data = history_five_db.find_one({"symbol": symbol})
    
    five_dict = str([{x: five_data['five_time_dict'][x]} for x in five_data['five_time_list'][-5:]])
    stock['five_dict'] = five_dict
    day_data = record_day_db.find({"symbol": symbol})
    stock['day_date'] = [x['date'] for x in day_data]

    stock['gsjj'] = stock.pop("gsjj")
    stock['jyfw'] = stock.pop("jyfw")
    stock = [{"key": k, "name": v} for k, v in stock.items()]
    return jsonify(stock)


@indexx.route('/get_index_list')
@jwt_required()
def get_index_list():
    key = request.args.get('key')
    key = key.split('_')[-1]
    if not isinstance(int(key), int):
        return jsonify([{"key": 'false'}])
    use_today = stock_db.find({"use": {"$gte": 1, '$lte': 5}, "today": int(key)})
    ret = []
    count = 0
    for i in use_today:
        count += 1
        d = {}
        d['symbol'] = i['symbol']
        d['name'] = i['name']
        d['ths_hxtc'] = i.get('ths_hxtc', i.get('hxtc', ''))

        day_data = record_day_db.find({"symbol": i['symbol']})
        d['date'] = str([x['date'] for x in day_data])

        d['macd'] = i['macd']
        d['high'] = i['today']
        ret.append(d)
        if count > 100:
            break

    
    return jsonify({"data": ret})